/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ExtractPublicKeyPanel.java
 *
 * Created on 02.02.2009, 16:46:55
 */
package jopensslgui;

import java.io.File;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

/**
 *
 * @author uli
 */
public class ExtractPublicKeyPanel extends javax.swing.JPanel
{

    /** Creates new form ExtractPublicKeyPanel */
    public ExtractPublicKeyPanel()
    {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        selectOutputFileButton = new javax.swing.JButton();
        outputFileField = new javax.swing.JTextField();
        outputFileLabel = new javax.swing.JLabel();
        selectPrivateKeyFileButton = new javax.swing.JButton();
        privateKeyFileField = new javax.swing.JTextField();
        privateKeyLabel = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        algorithmLabel = new javax.swing.JLabel();
        algorithmComboBox = new javax.swing.JComboBox();

        selectOutputFileButton.setText(i18n.getString("ExtractPublicKeyPanel.selectOutputFileButton.text")); // NOI18N
        selectOutputFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectOutputFileButtonselectRandomOutputFileButtonActionPerformed(evt);
            }
        });

        outputFileLabel.setText( i18n.getString("ExtractPublicKeyPanel.outputFileLabel.text")); // NOI18N

        selectPrivateKeyFileButton.setText(i18n.getString("ExtractPublicKeyPanel.selectPrivateKeyFileButton.text")); // NOI18N
        selectPrivateKeyFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectPrivateKeyFileButtonActionPerformed(evt);
            }
        });

        privateKeyFileField.setText( i18n.getString("ExtractPublicKeyPanel.privateKeyFileField.text")); // NOI18N

        privateKeyLabel.setText( i18n.getString("ExtractPublicKeyPanel.privateKeyLabel.text")); // NOI18N

        okButton.setText( i18n.getString("ExtractPublicKeyPanel.okButton.text")); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        algorithmLabel.setText( i18n.getString("ExtractPublicKeyPanel.algorithmLabel.text")); // NOI18N

        algorithmComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "RSA", "DSA" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(privateKeyLabel)
                                    .addGap(18, 18, 18))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(outputFileLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
                                    .addGap(7, 7, 7)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(algorithmLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(outputFileField, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(selectOutputFileButton))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(algorithmComboBox, javax.swing.GroupLayout.Alignment.LEADING, 0, 133, Short.MAX_VALUE)
                                    .addComponent(privateKeyFileField, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(selectPrivateKeyFileButton))))
                    .addComponent(okButton, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(algorithmLabel)
                    .addComponent(algorithmComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(privateKeyLabel)
                    .addComponent(privateKeyFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectPrivateKeyFileButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(outputFileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectOutputFileButton)
                    .addComponent(outputFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(okButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void selectOutputFileButtonselectRandomOutputFileButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_selectOutputFileButtonselectRandomOutputFileButtonActionPerformed
    {//GEN-HEADEREND:event_selectOutputFileButtonselectRandomOutputFileButtonActionPerformed
        fileChooser.setSelectedFile(new File(outputFileField.getText()));
        fileChooser.showSaveDialog(this);
        outputFileField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}//GEN-LAST:event_selectOutputFileButtonselectRandomOutputFileButtonActionPerformed

    private void selectPrivateKeyFileButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_selectPrivateKeyFileButtonActionPerformed
    {//GEN-HEADEREND:event_selectPrivateKeyFileButtonActionPerformed
        fileChooser.setSelectedFile(new File(privateKeyFileField.getText()));
        fileChooser.showSaveDialog(this);
        privateKeyFileField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}//GEN-LAST:event_selectPrivateKeyFileButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_okButtonActionPerformed
    {//GEN-HEADEREND:event_okButtonActionPerformed
        try
        {
            StringBuilder cmdBuilder = new StringBuilder("openssl -pubout ");
            cmdBuilder.append(algorithmComboBox.getSelectedItem().toString().toLowerCase());
            cmdBuilder.append(" -in " + privateKeyFileField.getText());
            cmdBuilder.append(" -out " + outputFileField.getText());

            logger.fine("Executing \'" + cmdBuilder.toString() + "\'");

            Process p = Runtime.getRuntime().exec(cmdBuilder.toString());
            p.waitFor();
        }
        catch (InterruptedException ex)
        {
            Logger.getLogger(ExtractPublicKeyPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex)
        {
            Logger.getLogger(ExtractPublicKeyPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_okButtonActionPerformed
    private JFileChooser fileChooser = new JFileChooser();
    private ResourceBundle i18n = ResourceBundle.getBundle("jopensslgui/Bundle"); //NOI18N
    private Logger logger = JOpenSSLFrame.logger;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox algorithmComboBox;
    private javax.swing.JLabel algorithmLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField outputFileField;
    private javax.swing.JLabel outputFileLabel;
    private javax.swing.JTextField privateKeyFileField;
    private javax.swing.JLabel privateKeyLabel;
    private javax.swing.JButton selectOutputFileButton;
    private javax.swing.JButton selectPrivateKeyFileButton;
    // End of variables declaration//GEN-END:variables
}
